package com.atguigu.flink.chapter07;

import com.atguigu.flink.chapter05.Source.WaterSensor;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * TODO
 *
 * @author cjp
 * @version 1.0
 * @date 2021/1/20 14:06
 */
public class Flink08_Watermark {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 1.12版本，默认就是 事件时间语义
        // 之前的版本，默认是 处理时间 语义， 如果要使用 事件时间，需要明确指定
//        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

        SingleOutputStreamOperator<WaterSensor> sensorDS = env
                .socketTextStream("localhost", 8888)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0], Long.valueOf(split[1]), Integer.valueOf(split[2]));
                    }
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forMonotonousTimestamps()     // 指定watermark生成(单调递增)
                                .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {   // 指定如何从数据中提取事件时间
                                    @Override
                                    public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                                        System.out.println("rts:"+recordTimestamp);
                                        return element.getTs() * 1000L; // 转换成 毫秒
                                    }
                                })
                );

        KeyedStream<WaterSensor, String> sensorKS = sensorDS.keyBy(sensor -> sensor.getId());

        WindowedStream<WaterSensor, String, TimeWindow> sensorWS = sensorKS
//                .window(TumblingProcessingTimeWindows.of(Time.seconds(10)));
                .window(TumblingEventTimeWindows.of(Time.seconds(10)));

        sensorWS
                .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    /**
                     *
                     * @param s 分组的key
                     * @param context   上下文
                     * @param elements  数据 => 可迭代类型，存了多个数据
                     * @param out   采集器
                     * @throws Exception
                     */
                    @Override
                    public void process(String s, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                        out.collect("key=" + s + "\n" +
                                "数据为:" + elements + "\n" +
                                "数量条数:" + elements.spliterator().estimateSize() + "\n" +
                                "窗口为:[" + context.window().getStart() + "," + context.window().getEnd() + ")\n" +
                                "=======================================================================\n\n");
                    }
                })
                .print();


        env.execute();
    }

}
/*
    Watermark概念：
        1、衡量 事件时间 的进展
        2、是一个 特殊的时间戳， 生成之后，随着 流的流动 而 向后传递
        3、用来处理 数据乱序 的问题
        4、触发 窗口等 的 计算、关闭
        5、单调递增的 （时间不能倒退）
        6、Flink认为，小于watermark时间戳的 数据 处理完了，不应该再出现
 */